package com.logic.landseaserver.ws;

import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.ConstantChar;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.models.payment.PaymentConstant.PayType;
import com.logic.landseaserver.service.IBillPaymentService;
import com.logic.landseaserver.ws.dto.BillPaymentDTO;
import com.logic.landseaserver.ws.dto.WeBankPayReqDTO;
import com.logic.landseaserver.ws.dto.WeBankPayRespDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/5/26
 * @Description: 账单支付控制类
 */
@Path("/billPayment")
@Api(value = "billPayment")
public class BillPaymentRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(BillPaymentRest.class);
    
    @Autowired
    private IBillPaymentService iBillPaymentService;
    
    // 增加
    @POST
    @Path("/createBillPayment")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "微众收银台支付前调用：iBillPaymentService.insertBillPaymentDTO(billPaymentDTO) ", position = 1)
    public Response createBillPayment(@ApiParam(value = "param", required = true) BillPaymentDTO billPaymentDTO)
    {
        LOGGER.info("BillPaymentRest|createBillPayment|Request|" + billPaymentDTO.toString());
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer id = iBillPaymentService.insertBillPaymentDTO(billPaymentDTO);
            billPaymentDTO.setId(id);
            map.setData(billPaymentDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "createBillPayment", e, map);
        }
        catch (Exception e)
        {
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 同步账单支付信息
    @POST
    @Path("/updatePayStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "同步账单支付信息", position = 1)
    public Response updatePayStatus(@ApiParam(value = "param", required = false) @QueryParam("billId") Integer billId)
    {
        LOGGER.info("BillPaymentRest|updatePayStatus|Request|billId=" + billId);
        CommonResultMap map = new CommonResultMap();
        try
        {
            String result = iBillPaymentService.updatePayStatus(billId);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS, "Update success");
        }
        catch (Exception e)
        {
            LOGGER.error("updatePayStatus error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/clearComeDue")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "测试清除已过期的账单", position = 14)
    public Response clearBillPaymentDue()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Date date = DateUtils.addMinutes(new Date(), -ConstantChar.deposit_clear_time);
            iBillPaymentService.clearBillPayment(date);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            LOGGER.error("clearBillPaymentDue error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 查询所有信息
    @SuppressWarnings("unchecked")
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询所以收款记录", position = 2)
    public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<BillPaymentDTO> billDTOList = null;
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            billDTOList = (List<BillPaymentDTO>)iBillPaymentService.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(billDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
            
        }
        catch (Exception e)
        {
            LOGGER.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 编辑
    @PUT
    @Path("/editBillPayment")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "微众收银台支付成功后调用：iBillPaymentService.editBillPaymentDTO(billPaymentDTO)")
    public Response updateOrder(@ApiParam(value = "param", required = true) BillPaymentDTO billPaymentDTO)
    {
        LOGGER.info("BillPaymentRest|updateOrder|Request|" + billPaymentDTO.toString());
        CommonResultMap map = new CommonResultMap();
        try
        {
            iBillPaymentService.editBillPaymentDTO(billPaymentDTO);
            map.setResult(CommonResult.SUCCESS, "edit activity");
        }
        catch (Exception e)
        {
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 详情
    @GET
    @Path("/query/{billPaymentId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据billPaymentId获取详情")
    public Response getBillPayment(
        @ApiParam(value = "param", required = true) @PathParam("billPaymentId") Integer billPaymentId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            BillPayment dto = iBillPaymentService.getBillPayment(billPaymentId);
            map.setData(dto);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 支付
    @POST
    @Path("/getWeBankPay")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "微众支付接口【 收单 & PayType.WEBANK_MOBILE(收银台)】")
    public Response getWeBankPay(@ApiParam(value = "param", required = true) WeBankPayReqDTO payinfo)
    {
        LOGGER.info("BillPaymentRest|getWeBankPay|Request|" + payinfo.toString());
        CommonResultMap map = new CommonResultMap();
        try
        {
            WeBankPayRespDTO paydto = null;
            paydto = iBillPaymentService.getBillWeBankPay(payinfo);
            map.setData(paydto);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getWeBankPay", e, map);
        }
        catch (Exception e)
        {
            LOGGER.error("getWeBankPay|process webank pay failed.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
